﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Text;
using QTMusic.BaseClasses;

namespace QTMusic.WebServices
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    /// 
    [ScriptService]
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        [ScriptMethod]
        public List<string> SuggestNames(string prefixText, int count)
        {
            SqlConnection conn = new SqlConnection(Common.connString);
            SqlCommand sql = new SqlCommand("SELECT * FROM [song] WHERE song_name like @prefix", conn);

            sql.Parameters.AddWithValue("@prefix", prefixText + "%");

            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(sql);
            sda.Fill(ds, "SuggestText");

            List<string> suggestResult = new List<string>();
            if (ds.Tables["SuggestText"].Rows.Count != 0)
            {
                foreach (DataRow dr in ds.Tables["SuggestText"].Rows)
                    suggestResult.Add(dr.Field<string>("song_name"));
            }
            conn.Close();

            return suggestResult;
        }

        [WebMethod]
        [ScriptMethod]
        public List<string> SuggestArtists(string prefixText, int count)
        {
            SqlConnection conn = new SqlConnection(Common.connString);
            SqlCommand sql = new SqlCommand("SELECT * FROM [artist] WHERE artist_name like @prefix", conn);

            sql.Parameters.AddWithValue("@prefix", prefixText + "%");

            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(sql);
            sda.Fill(ds, "SuggestText");

            List<string> suggestResult = new List<string>();
            if (ds.Tables["SuggestText"].Rows.Count != 0)
            {
                foreach (DataRow dr in ds.Tables["SuggestText"].Rows)
                    suggestResult.Add(dr.Field<string>("artist_name"));
            }
            conn.Close();

            return suggestResult;
        }

        [WebMethod]
        [ScriptMethod]
        public List<string> SuggestGenres(string prefixText, int count)
        {
            SqlConnection conn = new SqlConnection(Common.connString);
            SqlCommand sql = new SqlCommand("SELECT * FROM [genre] WHERE genre_name like @prefix", conn);

            sql.Parameters.AddWithValue("@prefix", prefixText + "%");

            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(sql);
            sda.Fill(ds, "SuggestText");

            List<string> suggestResult = new List<string>();
            if (ds.Tables["SuggestText"].Rows.Count != 0)
            {
                foreach (DataRow dr in ds.Tables["SuggestText"].Rows)
                    suggestResult.Add(dr.Field<string>("genre_name"));
            }
            conn.Close();

            return suggestResult;
        }

        [WebMethod]
        [ScriptMethod]
        public List<string> SuggestComposers(string prefixText, int count)
        {
            SqlConnection conn = new SqlConnection(Common.connString);
            SqlCommand sql = new SqlCommand("SELECT * FROM [composer] WHERE composer_name like @prefix", conn);

            sql.Parameters.AddWithValue("@prefix", prefixText + "%");

            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(sql);
            sda.Fill(ds, "SuggestText");

            List<string> suggestResult = new List<string>();
            if (ds.Tables["SuggestText"].Rows.Count != 0)
            {
                foreach (DataRow dr in ds.Tables["SuggestText"].Rows)
                    suggestResult.Add(dr.Field<string>("composer_name"));
            }
            conn.Close();

            return suggestResult;
        }

        [WebMethod]
        [ScriptMethod]
        public List<string> SuggestAlbums(string prefixText, int count)
        {
            SqlConnection conn = new SqlConnection(Common.connString);
            SqlCommand sql = new SqlCommand("SELECT * FROM [album] WHERE album_name like @prefix", conn);

            sql.Parameters.AddWithValue("@prefix", prefixText + "%");

            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(sql);
            sda.Fill(ds, "SuggestText");

            List<string> suggestResult = new List<string>();
            if (ds.Tables["SuggestText"].Rows.Count != 0)
            {
                foreach (DataRow dr in ds.Tables["SuggestText"].Rows)
                    suggestResult.Add(dr.Field<string>("album_name"));
            }
            conn.Close();

            return suggestResult;
        }
    }
}
